/*
 * Copyright 2007-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.asemantics.anthill;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;

import java.util.List;
import java.util.ArrayList;

/**
 * This class defines an <i>Ant</i> Task able to send
 * textual messages to a XMPP recipient.
 *
 * Here an example usage:
 *
 * <pre>
 *
 * &lt;notifier
 *    server="server-host"
 *    port="5222"
 *    security="enabled"
 *    username="user1@server-host"
 *    password="secret"
 *    debugger="false"
 *    compression="false"
 *    recipient="user-target@server-host"&gt;
 *
 *    &lt;message
 *        subject="Message subject"
 *        body="Message body"
 *    /&gt;
 *&lt;/notifier&gt;
 *
 * </pre>
 *
 */
public class NotifierTask extends AbstractConnectedTask {

    /**
     * Defines a textual message entry.
     */
    public class Message {

        /**
         * Message subject.
         */
        private String subject;

        /**
         * Message body.
         */
        private String body;

        /**
         * Constructor.
         */
        public Message() {}

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            validateMessageBody(body);
            this.body = body;
        }

        public void setText(String txt) {
            setBody(txt);
        }
    }

    /**
     * The recipient of the message.
     */
    private String recipient;

    /**
     * Available messages.
     */
    private List<Message> messages = new ArrayList<Message>();

    /**
     * Constructor.
     */
    public NotifierTask() {}

    /**
     * Adds a new message.
     * @return
     */
    public Message createMessage() {
        Message message = new Message();
        messages.add(message);
        return message;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }

    public void execute() throws BuildException {
        super.execute();

        if( messages.isEmpty() ) {
            throw new BuildException("Al least a message must be defined.");
        }

        for(Message message : messages) {
            log("Preparing messagePacket... ", Project.MSG_DEBUG);
            org.jivesoftware.smack.packet.Message messagePacket = new org.jivesoftware.smack.packet.Message();
            messagePacket.setSubject( message.getSubject() );
            messagePacket.setBody( message.getBody() );
            log("done", Project.MSG_DEBUG);
            log("Sending messagePacket... ", Project.MSG_DEBUG);
            sendMessage( createChat( recipient ), messagePacket);
            log("done", Project.MSG_DEBUG);
        }
    }

    /**
     * Validates the content of message body.
     *
     * @param body
     * @throws BuildException if the message body is not valid.
     */
    private void validateMessageBody(String body) {
        if( body == null || body.trim().length() == 0) {
            throw new BuildException("body cannot be null or blank string");
        }
    }

}
